package com.project.erp.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.project.erp.model.system.DocNumber;
import com.project.erp.service.system.DocNumberService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：系统 所有

 *  功能描述：各种编号生成工具类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-10 下午11:33:48
 */
public class SerialNumberUtils {
	
	/**
	 * 编号生成工具方法
	 * @param docNumber 
	 * @param secondName 中间名 例如:ALW13009 则secondName为13
	 * @param bits 后缀位数 例如:ALW13009 则bits为3
	 * @return
	 */
	public static String getSerialNumber(DocNumber docNumber,String secondName,Integer bits){
		DocNumberService docNumberService = SpringContextHolder.getBean("docNumberServiceImpl");
		String docNum = "";
		
		DocNumber docNumbe = docNumberService.getDocNumber(docNumber);
		if(docNumbe == null){
			docNumbe = new DocNumber();
			docNumbe.setDocName(docNumber.getDocName());
			docNumbe.setGeneYear(docNumber.getGeneYear());
			docNumbe.setGeneMonth(docNumber.getGeneMonth());
			docNumbe.setGeneDay(docNumber.getGeneDay());
			docNumbe.setNumCount(1L);
			docNumberService.save(docNumbe);
			
			docNum = docNumber.getDocName()+secondName+getZeros(bits,1)+"1";
		}else{
			DocNumber persistentDocNumber = docNumberService.findDocNumberById(docNumbe.getId());
			persistentDocNumber.setNumCount((persistentDocNumber.getNumCount()+1L));
			docNumberService.eidt(persistentDocNumber);

			String numCountStr = String.valueOf(persistentDocNumber.getNumCount());
			docNum = docNumber.getDocName()+secondName+getZeros(bits,numCountStr.length())+numCountStr;
		}
		
		return docNum;
	}
	
	/**
	 * 动态取得"0"的个数
	 * @param totallen 一共个数
	 * @param suffix 非0个数
	 * @return
	 */
	private static String getZeros(int totallen,int suffix){
		String zeros = "";
		int bits = totallen - suffix;
		for(int i=0;i<bits;i++){
			zeros += "0";
		}
		
		return zeros;
	}
	
	/**
	 * 取得发票日期 DEC.12,2013形式
	 * @return
	 */
	public static String getInvoiceDate(){
		String returnStr = "";
		String month = "";
		Date now = new Date();
		SimpleDateFormat yearformat = new SimpleDateFormat("yyyy");
		SimpleDateFormat monthformat = new SimpleDateFormat("MM");
		SimpleDateFormat dayformat = new SimpleDateFormat("dd");
		
		month = monthformat.format(now);
		if("01".equals(month)){
			returnStr += "JAN";
		}else if("02".equals(month)){
			returnStr += "FEB";
		}else if("03".equals(month)){
			returnStr += "MAR";
		}else if("04".equals(month)){
			returnStr += "APR";
		}else if("05".equals(month)){
			returnStr += "MAY";
		}else if("06".equals(month)){
			returnStr += "JUN";
		}else if("07".equals(month)){
			returnStr += "JUL";
		}else if("08".equals(month)){
			returnStr += "AUG";
		}else if("09".equals(month)){
			returnStr += "SEP";
		}else if("10".equals(month)){
			returnStr += "OTC";
		}else if("11".equals(month)){
			returnStr += "NOV";
		}else if("12".equals(month)){
			returnStr += "DEC";
		}
		returnStr += "."+dayformat.format(now)+","+yearformat.format(now);
		return returnStr;
	}
}
